const express = require('express')
const userCtrl = require('../controller/user')
const { body, validationResult } = require('express-validator')

const router = express.Router()

//用户登录
router.post('/users/login', userCtrl.login)

//用户注册
router.post('/users', [
  body('user.username').notEmpty().withMessage('用户名称不能为空'),
  body('user.password').notEmpty().withMessage('密码不能为'),
  body('user.email')
    .notEmpty().withMessage('邮箱不能为空')
    .isEmail().withMessage('邮箱格式不正确')
], (req, res, next)=>{
  const errors = validationResult(req)
  if(!errors.isEmpty()) {
    return res.status(404).json({errors: errors.array() })
  }
  next()
}, userCtrl.register)

//获取当前登录用户
router.get('/users', userCtrl.getCurrentUser)

//更新当前登录用户
router.put('/user', userCtrl.updateCurrentUser)



module.exports = router